global root_dir = "`1'"

include "$root_dir/code/config/config.do"


cap noi log using ${log_dir}/figure_3_wagetrend_countries.log, replace name(fig)

capture noi {

    * ------------------------------------------ *
    * Plot wages by country over time
    * ------------------------------------------ * 
foreach col in cl {
    use ${final_dir}/regression_dataset_from1970_tfacit1_countrylevel_resid.dta, clear
    qui include ${code_dir}/config/figuretools.do
    keep if inlist(country, "US", "DE", "FR", "GB", "JP", "IT")

    
    *set graphing parameters
    grstyle set margin ".25 2.3 .25 .25", cm: graph
    global color `col'

    if "${color}" == "default" {
        local cpl_name ""
        local color1 "`crm1'"
        local color2 "`crm2'"
        local color3 "`crm3'"
        local color4 "`crm4'"
        local color5 "`crm5'"
        local color6 "`crm6'"
    }
    if "${color}" == "bw" {
        local cpl_name "_bw"
        local color1 gs2
        local color2 gs4
        local color3 gs6
        local color4 gs8
        local color5 gs10
        local color6 gs10
    }
    if "${color}" == "cl" {
        colorpalette scico bamO, n(6) saturate(40) intensity(0.9) nograph  
        local cpl_name "_cl"
        local color1 "`r(p1)'"
        local color2 "`r(p2)'"
        local color3 "`r(p3)'"
        local color4 "`r(p4)'"
        local color5 "`r(p5)'"
        local color6 "`r(p6)'"
    }

    * A: log LSW

    preserve
    keep country year LSW_level
    reshape wide LSW_level, i(year) j(country, string)
    ren LSW_level* LSW*
    list LSW* if year == 2009

    twoway connected LSWUS LSWDE LSWJP LSWFR LSWGB LSWIT year, ///
    xlabel(1996 (4) 2009) ///
    ylab(#5, grid glpattern(dash) glwidth(thin) glcolor(gs14)) ///
    lpat(dash dash dash dash shortdash shortdash) ///
    msym(O D T S O D) ///
    msize(small small small small small small) ///
    lw(1.3pt 1.3pt 1.3pt 1.3pt 1.3pt 1.3pt) ///
    ytitle("Log low-skill wage") yscale(titlegap(*-4)) ///
    xtitle("Year") ///
    title("(A)  Low-skill wages " " ", position(6)) ///
    legend(off) /// 
    color("`color1'" "`color2'" "`color3'" "`color4'" "`color5'" "`color6'") ///
    text(2.615 2009.3 "United States", color(`color1') place(e) just(left)) text(3.03 2009.3 "Germany", color(`color2') place(e) just(left)) ///
    text(3.42 2009.3 "Japan", color(`color3') place(e) just(left)) text(3.555 2009.3 "France", color(`color4') place(e) just(left)) ///
    text(3.34 2009.3 "United" "Kingdom", color(`color5') place(e) just(left)) text(2.91 2009.3 "Italy", color(`color6') place(e) just(left)) ///
    name(lsw_level`cpl_name', replace)

    restore

    * B: log LSW residualized

    preserve
    gen y = 1
    *first, residualize
    reg LSW_level ib1995.year i.country_id
    predict LSW_cyfe, resid
    keep country year LSW_cyfe
    ren LSW_cyfe LSW
    reshape wide LSW, i(year) j(country, string)
    list LSW* if year == 2009

    twoway connected LSWUS LSWDE LSWJP LSWFR LSWGB LSWIT year, ///
    xlabel(1996 (4) 2009) ///
    ylab(#5, grid glpattern(dash) glwidth(thin) glcolor(gs14)) ///
    lpat(dash dash dash dash shortdash shortdash) ///
    msym(O D T S O D) ///
    msize(small small small small small small) ///
    lw(1.3pt 1.3pt 1.3pt 1.3pt 1.3pt 1.3pt) ///
    ytitle("Log low-skill wage") yscale(titlegap(*-4)) ///
    xtitle("Year") ///
    title("(B)  Low-skill wages" "residualized on country and year FE", position(6)) ///
    legend(off) /// 
    color("`color1'" "`color2'" "`color3'" "`color4'" "`color5'" "`color6'") ///
    text(-0.05 2009.3 "United States", color(`color1') place(e) just(left)) text(-.120 2009.3 "Germany", color(`color2') place(e) just(left)) ///
    text(0.0117 2009.3 "Japan", color(`color3') place(e) just(left)) text(0.12221 2009.3 "France", color(`color4') place(e) just(left)) ///
    text(0.1768 2009.3 "United" "Kingdom", color(`color5') place(e) just(left)) text(-0.0925 2009.3 "Italy", color(`color6') place(e) just(left)) ///
    name(lsw_resid`cpl_name', replace)

    restore


    * C: ISP residualized

    gen y = 1
    *first, residualize
    reg ISP_level ib1995.year i.country_id
    predict ISP_cyfe, resid
    keep country year ISP_cyfe
    ren ISP_cyfe ISP
    reshape wide ISP, i(year) j(country, string)
    list ISP* if year == 2009

    twoway connected ISPUS ISPDE ISPJP ISPFR ISPGB ISPIT year, ///
    xlabel(1996 (4) 2009) ///
    ylab(#5, grid glpattern(dash) glwidth(thin) glcolor(gs14)) ///
    lpat(dash dash dash dash shortdash shortdash) ///
    msym(O D T S O D) ///
    msize(small small small small small small) ///
    lw(1.3pt 1.3pt 1.3pt 1.3pt 1.3pt 1.3pt) ///
    ytitle("Log inverse skill-premium") yscale(titlegap(*-4)) ///
    xtitle("Year") ///
    title("(C)  Inverse skill-premium" "residualized on country and year FE", position(6)) ///
    legend(off) /// 
    color("`color1'" "`color2'" "`color3'" "`color4'" "`color5'" "`color6'") ///
    text(-0.040 2009.3 "United States", color(`color1') place(e) just(left)) text(-.065 2009.3 "Germany", color(`color2') place(e) just(left)) ///
    text(-0.006 2009.3 "Japan", color(`color3') place(e) just(left)) text(.029 2009.3 "France", color(`color4') place(e) just(left)) ///
    text(-0.020 2009.3 "United" "Kingdom", color(`color5') place(e) just(left)) text(0.101 2009.3 "Italy", color(`color6') place(e) just(left)) ///
    name(isp_resid`cpl_name', replace)

    * Graph combine
    grc1leg2 lsw_level`cpl_name' lsw_resid`cpl_name' isp_resid`cpl_name', xsize(14) ysize(6) loff row(1) 
    graph export ${fig_dir}/main/Figure_3_country_wage_trends`cpl_name'.pdf, replace

    grc1leg2 lsw_level`cpl_name' lsw_resid`cpl_name' isp_resid`cpl_name', xsize(14) ysize(6) loff row(1)
    graph export ${fig_dir}/main/fig3.eps, replace
}
}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close fig